package com.ruoyi.msg.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.msg.domain.bo.MsgPushBo;
import com.ruoyi.msg.domain.vo.MsgPushVo;
import com.ruoyi.msg.service.IMsgPushService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 消息推送
 *
 * @author xiaohao
 * @date 2023-02-01
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/msg/push")
public class MsgPushController extends BaseController {

    private final IMsgPushService iMsgPushService;

    /**
     * 查询消息推送列表
     */
    @GetMapping("/list")
    public TableDataInfo<MsgPushVo> list(MsgPushBo bo, PageQuery pageQuery) {
        return iMsgPushService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出消息推送列表
     */
    @Log(title = "消息推送", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(MsgPushBo bo, HttpServletResponse response) {
        List<MsgPushVo> list = iMsgPushService.queryList(bo);
        ExcelUtil.exportExcel(list, "消息推送", MsgPushVo.class, response);
    }

    /**
     * 获取消息推送详细信息
     *
     * @param id 主键
     */
    @GetMapping("/{id}")
    public R<MsgPushVo> getInfo(@NotNull(message = "主键不能为空")
                                @PathVariable Long id) {
        return R.ok(iMsgPushService.queryById(id));
    }

    /**
     * 新增消息推送
     */
    @SaCheckPermission("msg:push:add")
    @Log(title = "消息推送", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody MsgPushBo bo) {
        bo.setReadFlag("N");
        return toAjax(iMsgPushService.insertByBo(bo));
    }

    /**
     * 修改消息推送
     */
    @SaCheckPermission("msg:push:edit")
    @Log(title = "消息推送", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody MsgPushBo bo) {
        return toAjax(iMsgPushService.updateByBo(bo));
    }

    /**
     * 读取消息
     *
     * @param msgId
     * @return
     */
    @GetMapping("read/{msgId}")
    public R<Void> readMsg(@PathVariable("msgId") Long msgId) {
        MsgPushVo msgPush = iMsgPushService.queryById(msgId);
        if (msgPush != null) {
            msgPush.setReadFlag("Y");
            MsgPushBo update = BeanUtil.toBean(msgPush, MsgPushBo.class);
            iMsgPushService.updateByBo(update);
        }
        return R.ok();
    }

    /**
     * 删除消息推送
     *
     * @param ids 主键串
     */
    @SaCheckPermission("msg:push:remove")
    @Log(title = "消息推送", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iMsgPushService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
